package priv.pront.code.mati.province;

import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-11-17 15:21
 */
public class MC0215_小码哥玩游戏 {

    static int n , m, cnt = 0;
    static char c;
    static char[][] chs;
    static boolean[][] st;
    static int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        c = scanner.next().toCharArray()[0];
        chs = new char[n][m];
        st = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            chs[i] = scanner.next().toCharArray();
        }

        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if(chs[i][j] == '0') chs[i][j] = c;


        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(chs[i][j] != c) continue;
                process(i, j);
            }
        }
        System.out.println(cnt);
    }

    private static void process(int i, int j) {
        for (int k = 0; k < 4; k++) {
            int nx = i + dx[k];
            int ny = j + dy[k];
            if(nx < 0 || nx >= n || ny < 0 || ny >= m || st[nx][ny] || chs[nx][ny] == c) continue;
            st[nx][ny] = true;
            cnt++;
        }
    }
}
